﻿using System;
using System.Collections.Generic;
using System.Linq;
using ExpenseSystem.Common;
using ExpenseSystem.Fether;
using ExpenseSystem.Model;
using ExpenseSystem.Lor;

namespace ExpenseSystem.Fether
{
    public class VQuoteDetailFetcher : NormalFetcher<VQuoteDetailModel>
    {
        public VQuoteDetailModel GetDefault(long site, long region,long title,long salespoint,long production,out string message)
        {
            Restrain regionRest = Restrain.Eq("RegionId",region);
            Restrain titleRest = Restrain.Eq("TitleId", title);
            Restrain spRest = Restrain.Eq("SalesPointId", salespoint);
            Restrain productionRest = Restrain.Eq("ProductionId", production);
            Restrain restrain = Restrain.Or(Restrain.Or(regionRest,titleRest), spRest );
            restrain = Restrain.And(productionRest, Restrain.Or(Restrain.Eq("Global", true), restrain));
            restrain = Restrain.And(restrain,Restrain.Or(Restrain.Eq("State",Definition.STATE_CONFIRM), Restrain.Eq("State", Definition.STATE_APPROVED)));
            IList<VQuoteDetailModel> ls = List(site, OrderBy.GetDefault(), Limit.GetDefault(), out message, out rows, restrain);
            VQuoteDetailModel result = null;
            foreach(VQuoteDetailModel item in ls)
            {
                if (result == null) {
                    result = item;
                    continue;
                }
                if(result.Lv<item.Lv)
                {
                    result = item;
                }
            }
            return result;
        }
    }
}
